<% leaf = item.tree_node? ? item.leaf : item %>
<% data_checkboxes_tree_id = [check_boxes_tree_id, leaf.value].join("_") %>
<% is_blank_root = leaf.root? %>
<% data = item.tree_node? && item.node.present? ? { checkboxes_tree: data_checkboxes_tree_id } : {} %>
<% check_box_label = capture do %>
  <%= filter_text_for(leaf.label, id: "dropdown-title-#{data_checkboxes_tree_id}") %>
<% end %>

<% if is_blank_root %>
  <%= hidden_field_tag "#{form.object_name}[#{method}][]", "", id: "#{form.options[:namespace] rescue "default"}_filter_#{check_boxes_tree_id}_all" %>
<% end %>

<div class="filter">
  <%= form.check_box(
        method,
        check_boxes_tree_options(
          leaf.value,
          check_box_label,
          class: "reset-defaults",
          data:,
          is_root_check_box: is_blank_root,
          id: data_checkboxes_tree_id,
          namespace: form.options[:namespace],
          parent_id:,
          object: form.object
        ),
        leaf.value.to_s,
        nil
      ) %>
  <% if !is_blank_root && item.tree_node? && item.node.present? %>
    <button id="dropdown-trigger-<%= data_checkboxes_tree_id %>" data-controls="panel-dropdown-menu-<%= data_checkboxes_tree_id %>" aria-labelledby="dropdown-title-<%= data_checkboxes_tree_id %>">
      <%= icon "arrow-down-s-fill" %>
      <%= icon "arrow-up-s-fill" %>
    </button>
  <% end %>
</div>

<% if item.tree_node? && item.node.present? %>
  <% subitems_content = capture do %>
    <% item.node.each do |subitem| %>
      <%= form.dropdown_label(subitem, method, check_boxes_tree_id:, parent_id: data_checkboxes_tree_id || "") %>
    <% end %>
  <% end %>

  <%# Put the depending elements at the same level if the leaf is blank root (all) %>
  <% if is_blank_root %>
    <%= subitems_content %>
  <% else %>
    <%= content_tag :div, subitems_content, id: "panel-dropdown-menu-#{data_checkboxes_tree_id}" %>
  <% end %>
<% end %>
